package cn.yyh.pet.controller;

import cn.yyh.basic.exception.MyException;
import cn.yyh.basic.utils.ContextUtil;
import cn.yyh.basic.utils.PageInfo;
import cn.yyh.basic.utils.R;
import cn.yyh.pet.domain.Pet;
import cn.yyh.pet.domain.SearchMasterMsg;
import cn.yyh.pet.query.PetQuery;
import cn.yyh.pet.query.SearchMasterMsgQuery;
import cn.yyh.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: PetController
 * @Package: cn.yyh.pet.controller
 * @date 2021/7/6
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody PetQuery query, HttpServletRequest request){
        try {
            PageInfo<Pet> info = petService.pageQuery(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId());
            return R.oK().data("rows", info.getRows()).data("total",info.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PutMapping("/onsale")
    public R onSalePet(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PutMapping("/offsale")
    public R offSalePet(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("{id}")
    public R getPetDetailsById(@PathVariable Long id) {
        try {
            Pet pet = petService.getPetDetailsById(id);
            return R.oK().data("pet",pet);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("adopt/{id}")
    public R adoptPet(@PathVariable Long id,HttpServletRequest request) {
        try {
            petService.adopt(id, Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId());
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }
}
